package com.shengqin.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shengqin.common.pojo.CommonPage;
import com.shengqin.common.pojo.CommonResult;
import com.shengqin.config.CommonException;
import com.shengqin.entity.Standard;
import com.shengqin.entity.SysHealthProject;
import com.shengqin.listeners.ProjectDataListener;
import com.shengqin.listeners.StandardDataListener;
import com.shengqin.service.IStandardService;
import com.shengqin.service.SysHealthProjectService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("admin/v1/project")
public class HealthProjectController {

    @Resource
    private SysHealthProjectService projectService;
    @Resource
    private IStandardService standardService;


    @Operation(summary = "导入体测项目")
    @PostMapping("import")
    public CommonResult importProject(@RequestPart("file") MultipartFile file, @RequestParam Integer templateId) {
        try {
            projectService.removeByTemplateId(templateId);
            EasyExcel.read(file.getInputStream(), SysHealthProject.class, new ProjectDataListener(projectService, templateId)).sheet().doRead();
        } catch (ExcelDataConvertException e) {
            String message = String.format("第%s行，第%s列解析异常，请修改后重新上传", e.getRowIndex() + 1, e.getColumnIndex() + 1);
            throw new CommonException(message);
        } catch (Exception e) {
            throw new CommonException("上传失败，请重试。错误信息：" + e.getMessage());
        }
        return CommonResult.ok().setMsg("导入成功");
    }

    @Operation(summary = "导入评分标准")
    @PostMapping("importStandard")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult importStandard(@RequestPart("file") MultipartFile file, @RequestParam Integer templateId) {
        try {
            standardService.removeByTemplateId(templateId);
            EasyExcel.read(file.getInputStream(), Standard.class, new StandardDataListener(standardService, templateId)).sheet().doRead();
        } catch (ExcelDataConvertException e) {
            String message = String.format("第%s行，第%s列解析异常，请修改后重新上传", e.getRowIndex() + 1, e.getColumnIndex() + 1);
            throw new CommonException(message);
        } catch (Exception e) {
            throw new CommonException("上传失败，请重试。错误信息：" + e.getMessage());
        }
        return CommonResult.ok().setMsg("导入成功");
    }

    @PostMapping("/listByTemplateId")
    public CommonResult<List<SysHealthProject>> listByTemplateId(@RequestBody SysHealthProject project) {
        return CommonResult.data(projectService.listByPlanId(project));
    }

    @PostMapping("/page")
    public CommonResult<Page<SysHealthProject>> page(@RequestBody CommonPage<SysHealthProject> projectCommonPage) {
        return CommonResult.data(projectService.getPage(projectCommonPage));
    }

    @PostMapping("/save")
    public CommonResult<Boolean> save(@RequestBody SysHealthProject healthProject) {
        return CommonResult.data(projectService.saveOrUpdate(healthProject));
    }


    @PostMapping("/saveStandard")
    public CommonResult<Boolean> saveStandard(@RequestBody Standard standard) {
        return CommonResult.data(standardService.saveOrUpdate(standard));
    }

    @PostMapping("/standardPage")
    public CommonResult<Page<Standard>> standardPage(@RequestBody CommonPage<Standard> pageParam) {
        return CommonResult.data(standardService.getPage(pageParam));
    }

    @PostMapping("/delete")
    public CommonResult<Boolean> delete(@RequestBody SysHealthProject project) {
        return CommonResult.data(projectService.removeById(project.getId()));
    }

    @PostMapping("/deleteStandard")
    public CommonResult<Boolean> deleteStandard(@RequestBody Standard standard) {
        return CommonResult.data(standardService.removeById(standard.getId()));
    }

}
